<?php
// 检查是否已经开始会话
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

// 引入配置文件
$funAjaxPath = dirname(__DIR__) . '/fun.ajax.php';
if (file_exists($funAjaxPath)) {
    include_once $funAjaxPath;
} else {
    $_SESSION['message'] = '系统配置文件未找到';
    header('Location: index.php');
    exit;
}

// 检查登录状态
if (!isset($_SESSION['ADMINTOKEN']) || $_SESSION['ADMINTOKEN'] !== $conf['token']) {
    header('Location: index.php');
    exit;
}

// 获取消息
$message = $_SESSION['message'] ?? '';
unset($_SESSION['message']);

// 获取当前页面名称用于导航高亮
$currentPage = basename($_SERVER['PHP_SELF'], '.php');

// 获取访问统计数据（包含点击统计）
if (function_exists('getVisitStats')) {
    $visitStats = getVisitStats();
    // 修正字段名称映射
    $visitStats['today_ips'] = $visitStats['today_unique'] ?? 0;
    $visitStats['total_ips'] = $visitStats['total_unique'] ?? 0;
} else {
    $visitStats = [
        'today_visits' => 0,
        'today_unique' => 0,
        'today_ips' => 0,
        'total_visits' => 0,
        'total_unique' => 0,
        'total_ips' => 0,
        'today_clicks' => 0,
        'total_clicks' => 0
    ];
}
?>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理后台 - <?php echo htmlspecialchars($conf['title'] ?? '极简导航网'); ?></title>
    <link rel="stylesheet" href="https://assets.ithxs.com/cdn/font-awesome/all.min.css">
    <!-- Chart.js 图表库 -->
    <script src="https://assets.ithxs.com/cdn/Chart/4.5.0/chart.umd.min.js"></script>
    <style>
        :root {
            --primary-blue: #2563eb;
            --primary-blue-dark: #1d4ed8;
            --primary-blue-light: #3b82f6;
            --secondary-blue: #eff6ff;
            --accent-blue: #dbeafe;
            --success-green: #16a34a;
            --success-green-light: #dcfce7;
            --warning-yellow: #f59e0b;
            --warning-yellow-light: #fef3c7;
            --error-red: #dc2626;
            --error-red-light: #fee2e2;
            --white: #ffffff;
            --gray-50: #f8fafc;
            --gray-100: #f1f5f9;
            --gray-200: #e2e8f0;
            --gray-300: #cbd5e1;
            --gray-400: #94a3b8;
            --gray-500: #64748b;
            --gray-600: #475569;
            --gray-700: #334155;
            --gray-800: #1e293b;
            --gray-900: #0f172a;
            --border-radius: 8px;
            --border-radius-lg: 12px;
            --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
            --shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
            --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
            --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
            
            /* 磨砂玻璃效果变量 */
            --glass-bg: linear-gradient(135deg, rgba(255, 255, 255, 0.8) 0%, rgba(255, 255, 255, 0.7) 100%);
            --glass-bg-scrolled: linear-gradient(135deg, rgba(255, 255, 255, 0.9) 0%, rgba(255, 255, 255, 0.8) 100%);
            --glass-blur: blur(24px) saturate(180%) brightness(110%);
            --glass-blur-scrolled: blur(32px) saturate(200%) brightness(115%);
            --glass-border: 1px solid rgba(255, 255, 255, 0.2);
            --glass-border-scrolled: 1px solid rgba(255, 255, 255, 0.3);
            --glass-shadow: 0 8px 32px rgba(0, 0, 0, 0.08), 0 1px 0 rgba(255, 255, 255, 0.5) inset, 0 -1px 0 rgba(0, 0, 0, 0.05) inset;
            --glass-shadow-scrolled: 0 12px 40px rgba(0, 0, 0, 0.12), 0 2px 0 rgba(255, 255, 255, 0.6) inset, 0 -1px 0 rgba(0, 0, 0, 0.08) inset;
        }

        * {
            box-sizing: border-box;
        }

        html,
        body {
            overflow-x: hidden;
            width: 100%;
            max-width: 100vw;
        }

        /* 防止所有元素超出屏幕宽度 */
        * {
            max-width: 100%;
        }

        /* 确保所有容器元素不会超出父容器 */
        .container,
        .content,
        .panel,
        .card,
        .section {
            max-width: 100%;
            overflow-wrap: break-word;
            word-break: break-word;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background: var(--gray-50);
            margin: 0;
            padding: 0;
            color: var(--gray-800);
            line-height: 1.6;
        }

        /* 主布局 */
        .main-content {
            display: flex;
            min-height: 100vh;
            background: var(--gray-50);
        }

        /* 内容区域 */
        .content-area {
            flex: 1;
            margin-left: 260px;
            padding: 88px 24px 24px 24px; /* 顶部64px导航栏 + 24px间距 */
            transition: margin-left 0.3s ease;
            min-height: 100vh;
            background: var(--gray-50);
        }

        /* 顶部导航栏 - 磨砂玻璃效果 */
        .top-nav {
            background: var(--glass-bg);
            backdrop-filter: var(--glass-blur);
            -webkit-backdrop-filter: var(--glass-blur);
            border-bottom: var(--glass-border);
            padding: 0 24px;
            height: 64px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            z-index: 1000;
            box-shadow: var(--glass-shadow);
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            /* 兼容性回退 */
            filter: alpha(opacity=80);
            -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=80)";
        }

        /* 当页面滚动时增强磨砂效果 */
        .top-nav.scrolled {
            background: var(--glass-bg-scrolled);
            backdrop-filter: var(--glass-blur-scrolled);
            -webkit-backdrop-filter: var(--glass-blur-scrolled);
            border-bottom: var(--glass-border-scrolled);
            box-shadow: var(--glass-shadow-scrolled);
            transform: translateZ(0); /* 硬件加速 */
        }

        /* 磨砂玻璃效果的伪元素增强 */
        .top-nav::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: linear-gradient(90deg, 
                rgba(255, 255, 255, 0.1) 0%, 
                rgba(255, 255, 255, 0.05) 50%, 
                rgba(255, 255, 255, 0.1) 100%);
            pointer-events: none;
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .top-nav:hover::before {
            opacity: 1;
        }

        .nav-left {
            display: flex;
            align-items: center;
            gap: 16px;
        }

        .mobile-menu-toggle {
            display: none;
            background: none;
            border: none;
            font-size: 1.2rem;
            color: var(--gray-600);
            cursor: pointer;
            padding: 8px;
            border-radius: var(--border-radius);
            transition: all 0.2s ease;
        }

        .mobile-menu-toggle:hover {
            background: var(--gray-100);
            color: var(--gray-900);
        }

        .nav-title {
            font-size: 1.25rem;
            font-weight: 600;
            color: var(--gray-900);
            margin: 0;
        }

        .nav-right {
            display: flex;
            align-items: center;
            gap: 16px;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 8px;
            color: var(--gray-600);
            font-size: 0.9rem;
        }

        .logout-btn {
            background: var(--error-red);
            color: var(--white);
            border: none;
            padding: 8px 16px;
            border-radius: var(--border-radius);
            font-size: 0.9rem;
            cursor: pointer;
            transition: all 0.2s ease;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 6px;
        }

        .logout-btn:hover {
            background: #b91c1c;
            transform: translateY(-1px);
        }

        /* 侧边栏 - Ant Design风格 */
        .sidebar {
            background: var(--white);
            border-radius: 0;
            width: 260px;
            position: fixed;
            left: 0;
            top: 64px; /* 导航栏高度 */
            height: calc(100vh - 64px); /* 减去导航栏高度 */
            overflow-y: auto;
            border-right: 1px solid var(--gray-200);
            z-index: 200;
        }

        .sidebar-section {
            margin-bottom: 0;
        }

        .sidebar-section:last-child {
            margin-bottom: 0;
        }

        .sidebar-title {
            font-size: 0.75rem;
            font-weight: 600;
            color: var(--gray-500);
            text-transform: uppercase;
            letter-spacing: 0.05em;
            padding: 24px 20px 12px 20px;
            margin: 0;
            border-bottom: none;
        }

        .nav-menu {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .nav-item {
            margin: 0;
        }

        .nav-link {
            display: flex;
            align-items: center;
            justify-content: flex-start;
            padding: 12px 20px;
            color: var(--gray-700);
            text-decoration: none;
            font-size: 0.9rem;
            font-weight: 500;
            line-height: 1.5;
            min-height: 44px; /* 确保足够的点击区域 */
            transition: all 0.2s ease;
            border: none;
            border-radius: 0;
        }

        .nav-link:hover {
            background: var(--gray-50);
            color: var(--gray-900);
        }

        .nav-link.active {
            background: #e6f7ff;
            color: var(--primary-blue);
            border-right: 3px solid var(--primary-blue);
            font-weight: 600;
        }

        .nav-link i {
            width: 20px;
            margin-right: 12px;
            text-align: center;
            font-size: 16px;
            line-height: 1;
            display: inline-block;
            vertical-align: middle;
            flex-shrink: 0; /* 防止图标被压缩 */
        }

        /* 导航链接文字样式优化 */
        .nav-link span {
            display: inline-block;
            vertical-align: middle;
            line-height: 1.5;
        }

        /* 确保导航项目的最小高度和对齐 */
        .nav-item {
            margin: 0;
            position: relative;
        }

        .nav-item:not(:last-child) {
            border-bottom: 1px solid transparent;
        }

        /* 导航快速链接样式 */
        .nav-quick-link {
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .nav-item:hover .nav-quick-link {
            opacity: 1;
        }

        .nav-quick-link a {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 24px;
            height: 24px;
            border-radius: 4px;
            transition: all 0.3s ease;
        }

        .nav-quick-link a:hover {
            background-color: rgba(255, 255, 255, 0.1);
            color: #fff !important;
        }

        /* 面板样式 */
        .panel {
            background: var(--white);
            border-radius: var(--border-radius-lg);
            box-shadow: var(--shadow);
            margin-bottom: 24px;
            overflow: hidden;
        }

        .panel-header {
            padding: 24px;
            border-bottom: 1px solid var(--gray-200);
            background: var(--white);
        }

        .panel-title {
            font-size: 1.5rem;
            font-weight: 600;
            color: var(--gray-900);
            margin: 0 0 8px 0;
        }

        .panel-subtitle {
            color: var(--gray-600);
            margin: 0;
            font-size: 0.95rem;
        }

        .panel-content {
            padding: 24px;
        }

        /* 响应式设计 */
        @media (max-width: 1024px) {
            .content-area {
                margin-left: 240px;
            }

            .sidebar {
                width: 240px;
            }
        }

        @media (max-width: 768px) {
            .content-area {
                margin-left: 0;
                padding: 80px 16px 16px 16px; /* 顶部64px导航栏 + 16px间距 */
            }

            .sidebar {
                position: fixed;
                top: 64px; /* 导航栏高度 */
                left: -260px;
                width: 260px;
                height: calc(100vh - 64px); /* 减去导航栏高度 */
                background: var(--white);
                box-shadow: var(--shadow-xl);
                transition: left 0.3s ease;
                z-index: 1000;
            }

            .sidebar.open {
                left: 0;
            }

            .mobile-menu-toggle {
                display: block;
            }

            .sidebar-overlay {
                display: none;
                position: fixed;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                background: rgba(0, 0, 0, 0.5);
                z-index: 999;
            }

            .sidebar-overlay.show {
                display: block;
            }

            .sidebar-title {
                font-size: 0.7rem;
                padding: 12px 20px 6px 20px;
            }

            .sidebar-section {
                margin-bottom: 0;
            }

            .nav-link {
                padding: 12px 20px;
                font-size: 0.9rem;
            }

            .top-nav {
                padding: 0 16px;
            }

            .nav-title {
                font-size: 1.1rem;
            }

            .user-info {
                display: none;
            }
        }

        @media (max-width: 480px) {
            .content-area {
                padding: 76px 12px 12px 12px; /* 顶部64px导航栏 + 12px间距 */
            }

            .panel-header,
            .panel-content {
                padding: 16px;
            }

            .panel-title {
                font-size: 1.25rem;
            }

            .sidebar {
                padding: 0;
            }

            .nav-link {
                padding: 8px 10px;
                font-size: 0.8rem;
            }

            .sidebar-title {
                padding: 8px 10px 4px 10px;
            }
        }

        /* 消息提示样式 */
        .ant-message {
            position: fixed;
            top: 24px;
            left: 50%;
            transform: translateX(-50%);
            z-index: 9999;
            pointer-events: none;
        }

        .ant-message-notice {
            padding: 12px 16px;
            border-radius: var(--border-radius);
            box-shadow: var(--shadow-lg);
            margin-bottom: 8px;
            display: flex;
            align-items: center;
            gap: 8px;
            font-size: 0.9rem;
            font-weight: 500;
            pointer-events: auto;
            animation: antMessageSlideDown 0.3s ease;
        }

        .ant-message-success {
            background: var(--success-green-light);
            color: var(--success-green);
            border: 1px solid #a7f3d0;
        }

        .ant-message-error {
            background: var(--error-red-light);
            color: var(--error-red);
            border: 1px solid #fca5a5;
        }

        .ant-message-warning {
            background: var(--warning-yellow-light);
            color: var(--warning-yellow);
            border: 1px solid #fcd34d;
        }

        .ant-message-info {
            background: var(--accent-blue);
            color: var(--primary-blue);
            border: 1px solid #93c5fd;
        }

        @keyframes antMessageSlideDown {
            from {
                opacity: 0;
                transform: translateY(-20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
    </style>
</head>

<body>
    <!-- 消息提示容器 -->
    <div class="ant-message" id="ant-message-container"></div>

    <!-- 移动端侧边栏遮罩 -->
    <div class="sidebar-overlay" id="sidebar-overlay"></div>

    <main class="main-content">
        <!-- 侧边栏 -->
        <aside class="sidebar">
            <div class="sidebar-section">
                <h3 class="sidebar-title">导航菜单</h3>
                <ul class="nav-menu">
                    <!-- 预览 -->
                    <li class="nav-item">
                        <a href="dashboard.php" class="nav-link <?php echo $currentPage === 'dashboard' ? 'active' : ''; ?>">
                            <i class="fas fa-eye"></i>
                            <span>预览</span>
                        </a>
                    </li>
                    <!-- 网站设置 -->
                    <li class="nav-item">
                        <a href="settings.php" class="nav-link <?php echo $currentPage === 'settings' ? 'active' : ''; ?>">
                            <i class="fas fa-cog"></i>
                            <span>网站设置</span>
                        </a>
                    </li>

                    <li class="nav-item">
                        <a href="links.php" class="nav-link <?php echo $currentPage === 'links' ? 'active' : ''; ?>">
                            <i class="fas fa-link"></i>
                            <span>链接管理</span>
                        </a>
                    </li>
                    <li class="nav-item">
                        <a href="<?php echo $conf['project']['gitee_url']; ?>" target="_blank" class="nav-link" title="直接访问Gitee项目主页">
                            <i class="fas fa-code-branch"></i>
                            <span>项目信息</span>
                            <i class="fas fa-external-link-alt" style="font-size: 0.8em; margin-left: 4px;"></i>
                        </a>
                    </li>
                </ul>
            </div>
        </aside>

        <!-- 内容区域 -->
        <div class="content-area">
            <!-- 顶部导航 -->
            <nav class="top-nav">
                <div class="nav-left">
                    <button class="mobile-menu-toggle" id="mobile-menu-toggle">
                        <i class="fas fa-bars"></i>
                    </button>
                    <h1 class="nav-title">管理后台</h1>
                </div>
                <div class="nav-right">
                    <div class="user-info">
                        <i class="fas fa-user"></i>
                        <span>管理员</span>
                    </div>
                    <a href="index.php?action=logout" class="logout-btn">
                        <i class="fas fa-sign-out-alt"></i>
                        退出登录
                    </a>
                </div>
            </nav>

            <!-- 页面内容将在这里插入 -->
            <?php if ($message): ?>
                <script>
                    document.addEventListener('DOMContentLoaded', function() {
                        <?php
                        $messageType = 'info';
                        if (strpos($message, '成功') !== false || strpos($message, '保存') !== false) {
                            $messageType = 'success';
                        } elseif (strpos($message, '错误') !== false || strpos($message, '失败') !== false) {
                            $messageType = 'error';
                        } elseif (strpos($message, '警告') !== false) {
                            $messageType = 'warning';
                        }
                        ?>
                        AntMessage['<?php echo $messageType; ?>']('<?php echo addslashes($message); ?>');
                    });
                </script>
            <?php endif; ?>

            <script>
                // Ant Design 风格的消息提示组件
                const AntMessage = {
                    container: null,
                    
                    init() {
                        if (!this.container) {
                            this.container = document.getElementById('ant-message-container');
                        }
                    },
                    
                    show(type, content, duration = 3000) {
                        this.init();
                        
                        const notice = document.createElement('div');
                        notice.className = `ant-message-notice ant-message-${type}`;
                        
                        const icon = this.getIcon(type);
                        notice.innerHTML = `${icon}<span>${content}</span>`;
                        
                        this.container.appendChild(notice);
                        
                        // 自动移除
                        setTimeout(() => {
                            if (notice.parentNode) {
                                notice.style.opacity = '0';
                                notice.style.transform = 'translateY(-20px)';
                                setTimeout(() => {
                                    if (notice.parentNode) {
                                        this.container.removeChild(notice);
                                    }
                                }, 300);
                            }
                        }, duration);
                    },
                    
                    getIcon(type) {
                        const icons = {
                            success: '<i class="fas fa-check-circle"></i>',
                            error: '<i class="fas fa-times-circle"></i>',
                            warning: '<i class="fas fa-exclamation-triangle"></i>',
                            info: '<i class="fas fa-info-circle"></i>'
                        };
                        return icons[type] || icons.info;
                    },
                    
                    success(content, duration) {
                        this.show('success', content, duration);
                    },
                    
                    error(content, duration) {
                        this.show('error', content, duration);
                    },
                    
                    warning(content, duration) {
                        this.show('warning', content, duration);
                    },
                    
                    info(content, duration) {
                        this.show('info', content, duration);
                    }
                };

                // 移动端菜单功能
                function initializeMobileMenu() {
                    const menuToggle = document.getElementById('mobile-menu-toggle');
                    const sidebar = document.querySelector('.sidebar');
                    const overlay = document.getElementById('sidebar-overlay');

                    if (menuToggle) {
                        menuToggle.addEventListener('click', toggleMobileMenu);
                    }

                    if (overlay) {
                        overlay.addEventListener('click', hideMobileMenu);
                    }
                }

                function toggleMobileMenu() {
                    const sidebar = document.querySelector('.sidebar');
                    const overlay = document.getElementById('sidebar-overlay');

                    if (sidebar.classList.contains('open')) {
                        hideMobileMenu();
                    } else {
                        showMobileMenu();
                    }
                }

                function showMobileMenu() {
                    const sidebar = document.querySelector('.sidebar');
                    const overlay = document.getElementById('sidebar-overlay');

                    sidebar.classList.add('open');
                    overlay.classList.add('show');
                }

                function hideMobileMenu() {
                    const sidebar = document.querySelector('.sidebar');
                    const overlay = document.getElementById('sidebar-overlay');

                    sidebar.classList.remove('open');
                    overlay.classList.remove('show');
                }

                // 滚动时增强磨砂效果
                function handleNavScroll() {
                    const topNav = document.querySelector('.top-nav');
                    if (window.scrollY > 10) {
                        topNav.classList.add('scrolled');
                    } else {
                        topNav.classList.remove('scrolled');
                    }
                }

                // 页面加载完成后初始化
                document.addEventListener('DOMContentLoaded', function() {
                    initializeMobileMenu();
                    
                    // 监听滚动事件
                    window.addEventListener('scroll', handleNavScroll);
                    
                    // 初始检查滚动位置
                    handleNavScroll();
                });
            </script>
